#ifndef __SE_CONDITION_HPP__
#define __SE_CONDITION_HPP__ 1

#include "NonCopyable.hpp"
#include "MutexLock.hpp"
#include <pthread.h>

namespace se {

class Condition final : private NonCopyAble {
public:
    /**
     * @brief Construct a new Condition object
     * 
     * @param mutexLock 线程锁
     */
    explicit Condition(MutexLock &mutexLock) : _mutexLock(mutexLock) {
        ::pthread_cond_init(&this->_condition, nullptr);
    }

    /**
     * @brief 等待条件变量的通知
     * 
     */
    void wait() { ::pthread_cond_wait(&this->_condition, &this->_mutexLock._mutex); }

    /**
     * @brief 通知一个等待的条件变量
     * 
     */
    void notify() { ::pthread_cond_signal(&this->_condition); }

    /**
     * @brief 通知所有等待的条件变量
     * 
     */
    void notifyAll() { ::pthread_cond_broadcast(&this->_condition); }

    ~Condition() { ::pthread_cond_destroy(&this->_condition); }

private:
    pthread_cond_t _condition;
    MutexLock &_mutexLock;
};

}  // namespace se

#endif
